﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for clsDecon
/// </summary>
public class clsDecon:BLL
{
    DataClassesDataContext obj_decon = new DataClassesDataContext();
    private string MaDC;

    public string MaDC1
    {
        get { return MaDC; }
        set { MaDC = value; }
    }
    private int STT;

    public int STT1
    {
        get { return STT; }
        set { STT = value; }
    }

    private string Noidung;

    public string Noidung1
    {
        get { return Noidung; }
        set { Noidung = value; }
    }

    private string MaCH;

    public string MaCH1
    {
        get { return MaCH; }
        set { MaCH = value; }
    }

    private string Pa1;

    public string Pa11
    {
        get { return Pa1; }
        set { Pa1 = value; }
    }
    private string Pa2;

    public string Pa21
    {
        get { return Pa2; }
        set { Pa2 = value; }
    }
    private string Pa3;

    public string Pa31
    {
        get { return Pa3; }
        set { Pa3 = value; }
    }
    private string Pa4;

    public string Pa41
    {
        get { return Pa4; }
        set { Pa4 = value; }
    }
    private string Traloi;

    public string Traloi1
    {
        get { return Traloi; }
        set { Traloi = value; }
    }
    private int VitriDung;

    public int VitriDung1
    {
        get { return VitriDung; }
        set { VitriDung = value; }
    }

    public clsDecon()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    //*************************************************
    public List<clsDecon> hamRandomDapAn(List<clsDecon> ctch)
    {
        
        foreach (var item in ctch)
        {
            List<string> listswap = new List<string>();
            listswap.Add(item.Pa1);
            listswap.Add(item.Pa2);
            listswap.Add(item.Pa3);
            listswap.Add(item.Pa4);
            var rand = new Random();
            for (int i = listswap.Count - 1; i > 0; i--)
            {
                int n = rand.Next(i + 1);
                string temp = listswap[i];
                listswap[i] = listswap[n];
                listswap[n] = temp;


            }
            item.Pa1 = listswap[0];
            item.Pa2 = listswap[1];
            item.Pa3 = listswap[2];
            item.Pa4 = listswap[3];
            for (int i = 0; i < 4; i++)
            {
                if (string.Compare(listswap[i], item.Traloi) == 0)
                {
                    i = i + 1;
                    item.VitriDung = i;
                    break;
                }
            }
        }
        return ctch;
        //********************************************

    }

    //********************************************
    public List<clsDecon> hamTronCauhoi(List<clsDecon> ctch)
    {
        int gtstt = 1;

        var rand = new Random();
        for (int i = ctch.Count - 1; i > 0; i--)
        {
            int n = rand.Next(i + 1);
            clsDecon temp = ctch[i];
            ctch[i] = ctch[n];
            ctch[n] = temp;
        }
        foreach (var item in ctch)
        {
            item.STT = gtstt;
            gtstt++;
        }

        return ctch;

    }
    //********************************************

    public List<clsDecon> dscauhoi(string madt)
    {
        var ctch = (from p in obj_decon.tbl_NGANHANGCAUHOIs
                    from q in obj_decon.tbl_CHITIETDETHIs
                    where p.MaCH == q.MaCH &&
                         q.MaDT == madt
                    select new clsDecon
                    {

                        //STT = string.Empty,
                        Noidung=p.NoiDungCH,
                        MaCH = p.MaCH,
                        Pa1 = p.DA1,
                        Pa2 = p.DA2,
                        Pa3 = p.DA3,
                        Pa4 = p.DA4,
                        Traloi = p.DADUNG,
                        VitriDung = 0
                    }).ToList();
        List<clsDecon> dsdeconDatronDapAn = hamRandomDapAn(ctch);

        return hamTronCauhoi(dsdeconDatronDapAn);
    }
    public string TaoDeCon(string madt)
    {
        tbl_DECON decon = new tbl_DECON();
        int idmaxs = (int)obj_decon.sp_GetCurrentIndNew("tbl_DECON").ReturnValue;

        string madc = TaoMaAuto("MaDC", "0000", idmaxs);
        decon.MaDC = madc;
        decon.MaDT = madt;
        obj_decon.tbl_DECONs.InsertOnSubmit(decon);
        obj_decon.SubmitChanges();
        return madc;
 
    }
    public void TaoCTDeCon(string madt)
    {
        List<clsDecon> dsDECONResult = dscauhoi(madt);
        string madc = TaoDeCon(madt);
        foreach (var item in dsDECONResult)
        {
            tbl_CHITIETDC chitietdecon = new tbl_CHITIETDC();
            int idmaxs = (int)obj_decon.sp_GetCurrentIndNew("tbl_CHITIETDC").ReturnValue;

            string mactdc = TaoMaAuto("MaCTDC", "0000", idmaxs);
            chitietdecon.Noidung = item.Noidung;
            chitietdecon.MaCTDC = mactdc;
            chitietdecon.STT = item.STT;
            chitietdecon.PA1 = item.Pa1;
            chitietdecon.PA2 = item.Pa2;
            chitietdecon.PA3 = item.Pa3;
            chitietdecon.PA4 = item.Pa4;
            chitietdecon.MaDC = madc;
            chitietdecon.DA = item.VitriDung;
            obj_decon.tbl_CHITIETDCs.InsertOnSubmit(chitietdecon);
            obj_decon.SubmitChanges();
        }
        
    }
    public bool ktDataodecon(string madt)
    {
        bool valExists=obj_decon.tbl_DECONs.Contains(obj_decon.tbl_DECONs.FirstOrDefault(p=>p.MaDT==madt));
        return valExists;

 
    }
}
